<!--  -->

<style lang="less">
	.goods_detail {
		swiper {
			height: 376*2rpx;

			image {
				width: 100%;
			}


		}

		.left {
			display: -webkit-box;
			-webkit-line-clamp: 2;
			overflow: hidden;
			text-overflow: ellipsis;
			-webkit-box-orient: vertical;
		}

		.title {
			.content {
				display: flex;
				align-items: center;
			}

			.right {
				text {
					font-size: 26rpx;
				}
			}
		}

		.footer {
			display: flex;
			align-items: center;
		}
	}
</style>
<template>
	<view class="goods_detail">
		<swiper autoplay indicator-dots indicator-active-color="red" circular>
			<swiper-item v-for="(item,index) in detailswiper" :key="index">
				<image :src="item.pics_sma_url" mode="widthFix"></image>
			</swiper-item>
		</swiper>
		<view class="title">
			<view style="color: mediumvioletred;">￥{{titledatail.goods_price}}</view>
			<view class="content" style="display: flex; background-color: #FFFFFF; padding: 10rpx 10rpx;">
				<view class="left" style="flex: 1;">
					{{titledatail.goods_name}}
				</view>
				<view class="right" style="width: 20%; text-align: center;border-left: #000000 2rpx solid;">
					<view class="iconfont icon-shoucang" @click="shoucahng"></view>
					<text v-if="flagtext">已收藏</text><text v-else>收藏</text>
				</view>
			</view>
		</view>
		<view style="image{width: 100%;}" v-html="titledatail.goods_introduce">
		</view>
		<view class="footer"
			style="position: fixed; bottom: 0; left: 0; background-color: #FFFFFF; width: 100vw; display: flex; font-size: 30rpx;">
			<view style="flex: 1.3;text-align: center;">
				<text class="iconfont icon-lianxikefu"></text>
				<view>联系客服</view>
			</view>
			<view style="flex: 1.3;text-align: center;">
				<text class="iconfont icon-fenxiang"></text>
				<view>分享</view>
			</view>
			<navigator url="/pages/cate/cate" style="flex: 1.3;text-align: center;" open-type="switchTab">
				<text class="iconfont icon-gouwuche"></text>
				<view>购物车</view>
			</navigator>

			<view @click="CartAdd"
				style="flex: 2;text-align: center;line-height: 100rpx;background-color: #f0a413;color: #FFFFFF;">
				加入购物车
			</view>

			<view style="flex: 2;text-align: center;line-height: 100rpx;background-color: #e22f5a;color: #FFFFFF;">
				<navigator url="/pages/cate/cate" open-type="switchTab">
					立即购买
				</navigator>
			</view>

		</view>
	</view>
</template>

<script>
	import {
		api
	} from "@/utils/index.js"
	export default {
		data() {
			return {
				cid: 0,
				detailswiper: [],
				titledatail: {},
				flagtext: false
			}
		},
		onLoad(o) {
			this.cid = o.cid
			this.getdetail()
			let shouchanglist = uni.getStorageSync('shouchanglist') || []
			let index = shouchanglist.findIndex(item => item.id == o.cid)
			if (index == -1) {
				this.flagtext = false
			} else {
				this.flagtext = true
			}
		},
		methods: {
			getdetail() {
				api({
					url: `public/v1/goods/detail`,
					data: {
						goods_id: this.cid
					}
				}).then(res => {
					this.detailswiper = res.message.pics
					this.titledatail = res.message
				})
			},
			CartAdd() {
				let obj = {
					goods_name: this.titledatail.goods_name,
					img: this.titledatail.goods_small_logo,
					flag: false,
					num: 1,
					goods_price: this.titledatail.goods_price,
					goods_id: this.titledatail.goods_id
				}
				let data = uni.getStorageSync('list') || []
				let index = data.findIndex(item => item.goods_id == obj.goods_id)
				if (index > -1) {
					data[index].num++
				} else {
					data.push(obj)
				}
				uni.setStorageSync("list", data)
				// 弹窗
				uni.showToast({
					title: "添加成功",
					mask: true
				})
			},
			shoucahng() {
				let obj = {
					name: this.titledatail.goods_name,
					img: this.titledatail.goods_small_logo,
					price: this.titledatail.goods_price,
					id: this.titledatail.goods_id,
					promote: this.titledatail.is_promote
				}
				console.log(this.titledatail);
				let shouchanglist = uni.getStorageSync('shouchanglist') || []
				let index = shouchanglist.findIndex(item => item.id == obj.id)
				if (index == -1) {
					shouchanglist.push(obj)
					this.flagtext = true
					uni.showToast({
						title: "收藏成功",
						// 1.5秒点一次
						mask: true
					})
				} else {
					uni.showToast({
						title: "已经收藏过了",
						icon: 'none',
						// 1.5秒点一次
						mask: true
					})
				}
				uni.setStorageSync("shouchanglist", shouchanglist)

			}
		}
	}
</script>
